package com.loyal.mwm.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.annotation.StringRes;
import androidx.databinding.ViewDataBinding;

import com.loyal.mwm.impl.IContactImpl;

public abstract class BaseClickHandler<V extends ViewDataBinding> implements IContactImpl {
    protected ProgressDialog progressDialog;
    protected BaseActivity<V> activity;
    protected V binding;

    public BaseClickHandler(BaseActivity<V> baseActivity) {
        this(baseActivity, null);
    }

    public BaseClickHandler(BaseActivity<V> baseActivity, V binding) {
        this.activity = baseActivity;
        this.binding = binding;
        initDialog(baseActivity);
        hasIntentParams(false);
    }

    protected void hasIntentParams(boolean hasParam) {
    }

    public final String getString(@StringRes int resId) {
        return activity.getString(resId);
    }

    public final void setResult(int resultCode) {
        activity.setResult(resultCode);
    }

    public final void setResult(int resultCode, Intent intent) {
        activity.setResult(resultCode, intent);
    }

    private void initDialog(BaseActivity<V> baseActivity) {
        progressDialog = new ProgressDialog(baseActivity);
        progressDialog.setMessage("处理中...");
        Window window = progressDialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.7f;// 透明度
            lp.dimAmount = 0.8f;// 黑暗度
            window.setAttributes(lp);
        }
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
    }

    protected void clearText(EditText editText) {
        if (null == editText) return;
        editText.setText("");
    }
}
